package com.zzj.until;

import org.junit.Test;

import java.io.*;
import java.util.Enumeration;
import java.util.Properties;

/**
 * PropertiesUtil.java
 *
 * @author zzj
 * @desc properties 资源文件解析工具
 * @datatime
 */
public class PropertiesUtil {

    private String properiesName = "";

    public PropertiesUtil() {

    }

    public PropertiesUtil(String fileName) {
        this.properiesName = fileName;
    }

    /**
     * 按key获取值
     *
     * @param key
     * @return
     */
    public String readProperty(String key) {
        String value = "";
        InputStream is = null;
        try {
            is = new FileInputStream(properiesName);
            Properties p = new Properties();
            p.load(is);
            value = p.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

    /**
     * 按key获取值，若没有，则取defStr为默认值
     * @param key
     * @param defStr
     * @return
     */
    public String readProperty(String key,String defStr) {
        String value = "";
        InputStream is = null;
        try {
            is = new FileInputStream(properiesName);
            Properties p = new Properties();
            p.load(is);
            value = p.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(value == null ||  "".equals(value)){
            value=defStr;
        }
        return value;
    }
    /**
     * 获取整个配置信息
     *
     * @return
     */
    public Properties getProperties() {
        Properties p = new Properties();
        InputStream is = null;
        try {
            is = new FileInputStream(properiesName);
            p.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return p;
    }

    /**
     * key-value写入配置文件
     *
     * @param key
     * @param value
     */
    public void writeProperty(String key, String value) {
        InputStream is = null;
        OutputStream os = null;
        Properties p = new Properties();
        try {
            is = new FileInputStream(properiesName);
            p.load(is);
            os = new FileOutputStream(properiesName);

            p.setProperty(key, value);
            p.store(os, key);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is)
                    is.close();
                if (null != os)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

//    public static void main(String[] args) {
//        PropertiesUtil q = new PropertiesUtil("config/druid.properties");
//        // q.writeProperty("myUtils", "wang33");
//        q.readProperty("basePath");
//        System.out.println(q.readProperty("basePath"));
//    }



}